/*
 * VHWinnerListGrid.java
 * Created on 14 juil. 2009
 *
 * VirtualHockey Internet Platform
 * http://code.google.com/p/virtualhockey/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.client.ui.widget;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SortDir;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.hockeo.vhweb.client.VHMain;
import com.hockeo.vhweb.client.res.VHResConstants;
import com.hockeo.vhweb.client.rpc.VHRpcResultReceiver;
import com.hockeo.vhweb.client.utils.VHJsData;

/**
 * Widget displaying contest history records in table form.
 *
 * @version $Id$
 * @author jjanke
 */
public class VHWinnerListGrid extends LayoutContainer implements VHRpcResultReceiver<List<VHJsData>>
{
  private final ListStore<VHJsData> d_store;
  private Grid<VHJsData>            d_grid;

  public VHWinnerListGrid()
  {
    d_store = new ListStore<VHJsData>();

    buildGrid();
    setLayout( new FitLayout() );
    add( d_grid );
  }

  public void setRpcData( List<VHJsData> listRecords, String strPurpose )
  {
    d_store.removeAll();
    d_store.add( listRecords );
    d_store.sort( "season_id", SortDir.DESC );
  }

  private void buildGrid()
  {
    // Cell rendering
    //
    GridCellRenderer<VHJsData> renderer = new GridCellRenderer<VHJsData>() {
      public String render( VHJsData record, String strProperty, ColumnData config, int rowIndex,
          int colIndex, ListStore<VHJsData> store, Grid<VHJsData> grid )
      {
        if ( strProperty.equals( "clubWinner_image032Id" ) )
          return "<img src='ImageServlet?id=" + record.getInt( "clubWinner_image032Id" ) + "'/>";
        else if ( strProperty.equals( "clubWinner_country" ) )
          return "<img src='images/flags/32x32/" + record.getString( "clubWinner_country" ) + ".png'/>";
        else if ( strProperty.equals( "clubSecond_image032Id" ) )
          return "<img src='ImageServlet?id=" + record.getInt( "clubSecond_image032Id" ) + "'/>";
        else if ( strProperty.equals( "clubSecond_country" ) )
          return "<img src='images/flags/32x32/" + record.getString( "clubSecond_country" ) + ".png'/>";
        else if ( strProperty.equals( "clubWinner_name" ) )
          return "<span style='font-weight: bold;'>" + record.getString( "clubWinner_name" ) + "</span>";

        return String.valueOf( record.get( strProperty ) );
      }
    };

    // Column definitions
    //
    VHResConstants constants = VHMain.getConstants();
    ArrayList<ColumnConfig> listCols = new ArrayList<ColumnConfig>();
    ColumnConfig col;

    col = new ColumnConfig();
    col.setId( "season_id" );
    col.setHeader( constants.seasonIdAbbrev() );
    col.setToolTip( constants.seasonId() );
    col.setWidth( 30 );
    col.setRenderer( renderer );
    col.setAlignment( HorizontalAlignment.RIGHT );
    col.setResizable( false );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "season_name" );
    col.setHeader( constants.seasonNameAbbrev() );
    col.setToolTip( constants.seasonName() );
    col.setWidth( 70 );
    col.setRenderer( renderer );
    col.setAlignment( HorizontalAlignment.LEFT );
    col.setResizable( false );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "clubWinner_image032Id" );
    col.setHeader( constants.clubLogoAbbrev() );
    col.setToolTip( constants.clubLogo() );
    col.setWidth( 42 );
    col.setRenderer( renderer );
    col.setAlignment( HorizontalAlignment.CENTER );
    col.setResizable( false );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "clubWinner_country" );
    col.setHeader( constants.countryAbbrev() );
    col.setToolTip( constants.country() );
    col.setWidth( 42 );
    col.setRenderer( renderer );
    col.setAlignment( HorizontalAlignment.CENTER );
    col.setResizable( false );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "clubWinner_name" );
    col.setHeader( constants.winnerList1st() );
    col.setWidth( 225 );
    col.setRenderer( renderer );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "clubSecond_image032Id" );
    col.setHeader( constants.clubLogoAbbrev() );
    col.setToolTip( constants.clubLogo() );
    col.setWidth( 42 );
    col.setRenderer( renderer );
    col.setAlignment( HorizontalAlignment.CENTER );
    col.setResizable( false );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "clubSecond_country" );
    col.setHeader( constants.countryAbbrev() );
    col.setToolTip( constants.country() );
    col.setWidth( 42 );
    col.setRenderer( renderer );
    col.setAlignment( HorizontalAlignment.CENTER );
    col.setResizable( false );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "clubSecond_name" );
    col.setHeader( constants.winnerList2nd() );
    col.setWidth( 225 );
    col.setRenderer( renderer );
    listCols.add( col );

    final ColumnModel cm = new ColumnModel( listCols );

    // Grid
    //
    d_grid = new Grid<VHJsData>( d_store, cm );
    d_grid.setBorders( true );
    d_grid.setAutoWidth( true );
    d_grid.setStripeRows( true );
  }
}
